package com.hackordie.controllers;

import java.security.Principal;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hackordie.commons.authorities.Profile;
import com.hackordie.commons.authorities.User;
import com.hackordie.exceptions.HackOrDieException;

@Controller
@RequestMapping(value = "/user")
public class UserController extends AMainController {

	@RequestMapping(value = "/dashboard", method = RequestMethod.GET)
	public String login(ModelMap model, HttpServletRequest request) throws Exception {
		breadCrumbService.addNode("User", 0, request);
		return "user/dashboard";
	}

	@RequestMapping(value = "/profile", method = RequestMethod.GET)
	public String profile(ModelMap model, Principal principal, HttpServletRequest request) throws HackOrDieException {
		breadCrumbService.addNode("Profile", 1, request);
		User user = hodManager.getUser(principal.getName());
		Profile profile = null;
		
		try {
			profile = hodManager.getProfile(user.getIdUser());
		} catch (HackOrDieException e) {
			return "redirect:/profile/create";
		}

		model.addAttribute("user", user);
		model.addAttribute("profile", profile);
		return "user/profile";

	}

	@RequestMapping(value = "/profile/create", method = RequestMethod.GET)
	public String profileCreate(ModelMap model, Principal principal, HttpServletRequest request) throws Exception {
		breadCrumbService.addNode("Create Profile", 1, request);
		return "user/create_profile";
	}

	@RequestMapping(value = "/profile/{idUser}", method = RequestMethod.GET)
	public String profileId(@PathVariable("idUser") int idUser, ModelMap model, HttpServletRequest request) throws Exception {
		breadCrumbService.addNode("Profile", 1, request);
		User user = hodManager.getUser(idUser);
		Profile profile = hodManager.getProfile(user.getIdUser());
		model.addAttribute("user", user);
		model.addAttribute("profile", profile);
		return "user/profile";
	}
}
